<template>
	<view class="my-info">
		<view class="info-avator">
			<image class="avatar" :src="userInfo.avatar || '/static/img/avatar.png'"></image>
			<view class="info-box">
				<text class="username" @tap="getUserInfo">{{userInfo.nickname || '点击登录'}}</text>
				<!-- <text class="phone" open-type="getPhoneNumber"  @click="getMobile" >{{  userInfo.mobile || '请绑定手机号' }}</text> -->
				<button 
				 v-if="userInfo.nickname"
				 
				 class="phone"
				 open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
				    {{ userInfo.mobile || '请绑定手机号' }}
				 </button>
			</view>
			<view class="info-manage">
				<image class="ewm" :src="'/static/img/ewm.png'"></image>
				<image class="to-info" :src="'/static/img/to.png'"></image>
			</view>
		</view>
		<!-- <view class="info-card" v-if="vipCard.cardId">
			<view class="vip-card" @tap="toCount()">
				<view class='ribbon'>
					<span>银卡</span>
				</view>
				<view style="padding:15px">
					<uni-row >
					    <uni-col :span="6">
							姓名
						</uni-col>
						<uni-col :span="16">
							{{vipCard.realName}}
						</uni-col>
					</uni-row>
					<uni-row >
					    <uni-col :span="6">
							生日
						</uni-col>
						<uni-col :span="16">
							{{vipCard.birth}}
						</uni-col>
					</uni-row>
					<view style="float: right;margin-top: 15rpx;">
						NO.{{vipCard.idcard}}
					</view>
				</view>
			</view>
		</view> -->
		
		<!-- <view class="info-card-button" v-else>
			<view v-if="userInfo.nickname">
				<view class="card-button" @click="gotoVipCard">您还未领取会员卡，点击办理会员卡~</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	export default {
		data(){
			return {
				userInfo: {},
				vipCard: {},
			}
		},
		created(){
			this.userInfo = uni.getStorageSync("user_info") || {};
			// this.vipCard = uni.getStorageSync("card_info") || {};
			// if(JSON.stringify(this.vipCard) == '{}' && 
			// JSON.stringify(this.userInfo) != '{}'){
			// 	this.getVipCardInfo();
			// }
		},
		methods:{
			getUserInfo(res){
				let _this = this;
				// 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
				wx.getUserProfile({
				  desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				  success: (res) => {
					console.log(res);
					if (res.userInfo) {
						_this.$api.setUserInfo({
							nickname: res.userInfo.nickName,
							avatar: res.userInfo.avatarUrl,
							position: res.userInfo.city + "/" +res.userInfo.province + "/"+res.userInfo.country ,
						}).then(res => {
							_this.userInfo = res.result;
							uni.setStorageSync("user_info", res.result);
							// _this.getVipCardInfo();
							_this.$emit("get-login");
						})
					} else {
					  wx.showToast({
						title: '授权失败，请重新授权！',
						icon: 'none',
						duration: 2000
					  });
					}
				  } 
				})
			},
			getPhoneNumber(e){
				console.log(e);
				if(e.detail.errMsg == 'getPhoneNumber:ok'){
				  let _this = this;
				  _this.$api.updateMobile({
					encryptedData: e.detail.encryptedData,
					ivData: e.detail.iv
				  }).then(res => { //成功回调
				  console.log(res);
					if (res.success) {
					  var data = res.result;
					  _this.userInfo.mobile = data.mobile;
					  uni.setStorageSync("user_info", _this.userInfo);
					}
				  }).catch(err => { }); //异常回调
				} 
			},
		// 	getVipCardInfo(){
		// 		let _this = this;
		// 		_this.$api.getVipCardInfo().then(res => {
		// 			if(res.success){
		// 				_this.vipCard = res.result;
		// 				uni.setStorageSync("card_info", res.result);
		// 			}
					
		// 		})
		// 	},
		// 	gotoVipCard(){
		// 		uni.navigateTo({
		// 			url: '/pages/my/my-info/vip-card/card'
		// 		})
		// 	},
		// 	toCount(){
		// 		uni.navigateTo({
		// 			url:'/pages/my/my-service/count/count'
		// 		})
		// 	}
		}
	}
</script>

<style lang="scss">
	@import './my-info.css'
	
</style>
